package com.zsy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsy.mapper.ClazzMapper;
import com.zsy.pojo.Clazz;
import com.zsy.pojo.ClazzQueryParam;
import com.zsy.pojo.PageResult;
import com.zsy.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-11 09:28
 **/
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    /**
     * 分页 + 条件查询
     *
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageResult<Clazz> clazzQueryParam(ClazzQueryParam clazzQueryParam) {
        // 1.设置分页
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        // 2.执行查询
        List<Clazz> clazzList = clazzMapper.clazzQueryParam(clazzQueryParam);
        for (Clazz clazz : clazzList) {
            clazz.setStatus("在读中");
            if (clazz.getEndDate().isBefore(LocalDate.now()))
                clazz.setStatus("已结课");
            if (clazz.getBeginDate().isAfter(LocalDate.now()))
                clazz.setStatus("已结课");
        }
        // 3.封装数据
        PageInfo<Clazz> pageInfo = new PageInfo<>(clazzList);
        // 4.返回数据
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    public void addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);
    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz queryClazzById(Integer id) {
       return clazzMapper.queryClazzById(id);
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }
}
